<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsLog extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['gl.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $goods_id = $this->request->post('goods_id');
            if(!empty($goods_id)){
                $where['gl.goods_id'] = ['in',$goods_id];
            }
            $stores_id = $this->request->post('stores_id');
            if(!empty($stores_id)){
                $where['gl.stores_id'] = ['in',$stores_id];
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_log')->alias('gl')
                    ->join('kuyou_goods g','g.id=gl.goods_id')
                    ->join('kuyou_stores s','s.id=gl.stores_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->order(['gl.business_type'=>'ASC','gl.id'=>'ASC'])
                    ->field('g.goods_code,g.goods_name,g.goods_type_id,g.goods_barcode,g.goods_image,g.goods_specification_id,g.note,g.unit_id,s.name as store_name,
                gl.purchase_code,gl.purchase_id,FROM_UNIXTIME(gl.purchase_time,\'%Y-%m-%d\') as purchase_time,gl.business_type,gl.customer_id,gl.goods_id,
                gl.attribute_id,gl.goods_unit,gl.remark,gl.stores_id,gl.enter_number,gl.enter_basics,gl.enter_unit,gl.enter_cost,gl.out_number,gl.out_basics,
                gl.out_unit,gl.out_cost,gl.balance_basics,gl.balance_unit,gl.balance_cost')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_log')->alias('gl')
                    ->join('kuyou_goods g','g.id=gl.goods_id')
                    ->join('kuyou_stores s','s.id=gl.stores_id')
                    ->where($where)
                    ->order(['gl.business_type'=>'ASC','gl.id'=>'ASC'])
                    ->field('g.goods_code,g.goods_name,g.goods_type_id,g.goods_barcode,g.goods_image,g.goods_specification_id,g.note,g.unit_id,s.name as store_name,
                gl.purchase_code,gl.purchase_id,FROM_UNIXTIME(gl.purchase_time,\'%Y-%m-%d\') as purchase_time,gl.business_type,gl.customer_id,gl.goods_id,
                gl.attribute_id,gl.goods_unit,gl.remark,gl.stores_id,gl.enter_number,gl.enter_basics,gl.enter_unit,gl.enter_cost,gl.out_number,gl.out_basics,
                gl.out_unit,gl.out_cost,gl.balance_basics,gl.balance_unit,gl.balance_cost')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_log')->alias('gl')
                ->join('kuyou_goods g','g.id=gl.goods_id')
                ->join('kuyou_stores s','s.id=gl.stores_id')
                ->where($where)->count();

            //获取设置
            $set= Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];

            $format = [
                'enter_number'  =>$number_decimal,
                'enter_basics'  =>$number_decimal,
                'enter_unit'    =>$price_decimal,
                'enter_cost'    =>$price_decimal,
                'out_number'    =>$number_decimal,
                'out_basics'    =>$number_decimal,
                'out_unit'      =>$price_decimal,
                'out_cost'      =>$price_decimal,
            ];
            $goodsData = [];
            foreach ($list as $list_k=>$list_v){
                //计算结存
                if(!isset($goodsData[$list_v['goods_id']])){
                    switch ($list_v['business_type']){
                        case 1:
                            //期初
                            $goodsData[$list_v['goods_id']] = ['basics'=>$list_v['balance_basics'],'cost'=>$list_v['balance_cost']];
                            break;
                        case 2:
                            //购货
                            $goodsData[$list_v['goods_id']] = ['basics'=>$list_v['enter_basics'],'cost'=>$list_v['enter_cost']];
                            break;
                        case 3:
                            //购货退货
                            $goodsData[$list_v['goods_id']] = ['basics'=>-$list_v['out_basics'],'cost'=>-$list_v['out_cost']];
                            break;
                        case 4:
                            //销货
                            $goodsData[$list_v['goods_id']] = ['basics'=>-$list_v['out_basics'],'cost'=>-$list_v['out_cost']];
                            break;
                        case 5:
                            //销货退货
                            $goodsData[$list_v['goods_id']] = ['basics'=>$list_v['enter_basics'],'cost'=>$list_v['enter_cost']];
                            break;
                        case 6:
                            //调拨
                            if($list_v['enter_basics']>0){
                                $goodsData[$list_v['goods_id']] = ['basics'=>$list_v['enter_basics'],'cost'=>$list_v['enter_cost']];
                            }else if($list_v['out_basics']>0){
                                $goodsData[$list_v['goods_id']] = ['basics'=>-$list_v['out_basics'],'cost'=>-$list_v['out_cost']];
                            }
                            break;
                        case 7:
                            //成本调整
                            $goodsData[$list_v['goods_id']] = ['basics'=>'','cost'=>$list_v['enter_cost']];
                            break;
                        case 8:
                            //盘盈
                            $goodsData[$list_v['goods_id']] = ['basics'=>$list_v['enter_basics'],'cost'=>$list_v['enter_cost']];
                            break;
                        case 9:
                            //其他入库
                            $goodsData[$list_v['goods_id']] = ['basics'=>$list_v['out_basics'],'cost'=>$list_v['out_cost']];
                            break;
                        case 10:
                            //盘亏
                            $goodsData[$list_v['goods_id']] = ['basics'=>-$list_v['out_basics'],'cost'=>-$list_v['out_cost']];
                            break;
                    }
                }else{
                    switch ($list_v['business_type']){
                        case 1:
                            //期初
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['balance_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['balance_cost']
                            ];
                            break;
                        case 2:
                            //购货
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['enter_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['enter_cost']
                            ];
                            break;
                        case 3:
                            //购货退货
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']-(float)$list_v['out_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']-(float)$list_v['out_cost']
                            ];
                            break;
                        case 4:
                            //销货
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']-(float)$list_v['out_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']-(float)$list_v['out_cost']
                            ];
                            break;
                        case 5:
                            //销货退货
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['enter_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['enter_cost']
                            ];
                            break;
                        case 6:
                            //调拨
                            if($list_v['enter_basics']>0){
                                $goodsData[$list_v['goods_id']] = [
                                    'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['enter_basics'],
                                    'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['enter_cost']
                                ];
                            }else if($list_v['out_basics']>0){
                                $goodsData[$list_v['goods_id']] = [
                                    'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']-(float)$list_v['out_basics'],
                                    'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']-(float)$list_v['out_cost']
                                ];
                            }
                            break;
                        case 7:
                            //购货
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['enter_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['enter_cost']
                            ];
                            break;
                        case 8:
                            //盘盈
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['balance_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['balance_cost']
                            ];
                            break;
                        case 9:
                            //其他入库
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']+(float)$list_v['enter_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']+(float)$list_v['enter_cost']
                            ];
                            break;
                        case 10:
                            //盘亏
                            $goodsData[$list_v['goods_id']] = [
                                'basics'=>(float)$goodsData[$list_v['goods_id']]['basics']-(float)$list_v['out_basics'],
                                'cost'=>(float)$goodsData[$list_v['goods_id']]['cost']-(float)$list_v['out_cost']
                            ];
                            break;
                    }
                }
                $list[$list_k]['balance_basics']    = isset($goodsData[$list_v['goods_id']]['basics'])?$goodsData[$list_v['goods_id']]['basics']:0;
                if(isset($goodsData[$list_v['goods_id']]['basics']) && $goodsData[$list_v['goods_id']]['basics']>0){
                    $list[$list_k]['balance_unit']  = round(($goodsData[$list_v['goods_id']]['cost']/$goodsData[$list_v['goods_id']]['basics']),$price_decimal);
                }else{
                    $list[$list_k]['balance_unit']  = 0;
                }
                $list[$list_k]['balance_cost']      = isset($goodsData[$list_v['goods_id']]['cost'])?$goodsData[$list_v['goods_id']]['cost']:0;

                //格式化数字输出
                foreach ($format as $k=>$v){
                    if(isset($list_v[$k]) && $list_v[$k]!==null){
                        $list[$list_k][$k]  = round($list_v[$k],$v);
                    }
                }

                $attribute_id = Db::name('kuyou_goods_attr')->where(['id'=>$list_v['attribute_id']])->value('attribute_id');
                $attribute_ids = Db::name('kuyou_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');
                if($attribute_ids){
                    $attribute_ids = explode('&',$attribute_ids);
                    $attribute = Db::name('kuyou_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');
                    $attribute_name = implode('/',$attribute);
                    $list[$list_k]['goods_name'] = $list_v['goods_name'].'_'.$attribute_name;
                }
                //商品类别
                $type_name = Db::name('kuyou_goods_type')->where(['id'=>$list_v['goods_type_id']])->value('supplier_type_name');
                $list[$list_k]['type_name'] = $type_name;
                //供应商/客户
                if($list_v['business_type']==2 || $list_v['business_type']==3){
                    $list[$list_k]['customer_id'] = Db::name('kuyou_suppliers')->where(['id'=>$list_v['customer_id']])->value('supplyer_name');
                }else if($list_v['business_type']==4 || $list_v['business_type']==5){
                    $list[$list_k]['customer_id'] = Db::name('kuyou_customers')->where(['id'=>$list_v['customer_id']])->value('customer_name');
                }
                //商品单位
                $unit = Db::name('kuyou_unit')->where(['id'=>$list_v['goods_unit']])->field('id,name')->find();
                $list[$list_k]['unit_name'] = isset($unit['name'])?$unit['name']:'';
                if($list_v['goods_unit']!=$list_v['unit_id']){//多单位
                    $more_unit = Db::name('kuyou_unit_conversion')->where(['main_unit'=>$list_v['unit_id'],'vice_unit'=>$list_v['goods_unit']])->find();
                    $unit_name = Db::name('kuyou_unit')->where(['id'=>$list_v['unit_id']])->value('name');
                    if($more_unit){
                        $list[$list_k]['unit_name'] = $list[$list_k]['unit_name'].'('.$more_unit['advance_rate'].$unit_name.')';
                    }
                }
            }
            $cost = 0;$basics = 0;
            foreach ($goodsData as $goodsData_k=>$goodsData_v){
                $cost   += (float)$goodsData_v['cost'];
                $basics += (float)$goodsData_v['basics'];
            }
            $basics  = round($basics,$number_decimal);$cost  = round($cost,$price_decimal);
            $result = array("count" => $row_count, "list" => $list, "basics" => $basics, "balance" => $cost);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
